package com.slife.serial.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.slife.serial.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;


@Component
public class HttpTaskService {


    private String app_key = "67cb0b9bf24c4d46be597bef1a4ce373";

    private String app_secret = "NItadupqdZbT1WOfI1sEHpUNtwl2KeWnxpeNDPXQIa9pFQfsJ0AT38jBNbViP6Ag";
    private String split_flag = "#";
    public String sendPost(JSONObject item1){
        StringBuilder sb = new StringBuilder();
        sb.append(app_secret);
        sb.append(split_flag);
        sb.append("app_key");
        sb.append(app_key);

        String dtype = "1";
        SortedMap<Object, Object> params = new TreeMap<Object, Object>();
        params.put("dtype", dtype);
        // 该参数放在 post 请求参数的前面 请求路径上如果参数多余固定参数 则 ASCII 排序
        sb.append(sortAscii(params));

        // 拼接前后固定值
        sb.append("timestamp");
        String timestamp = Long.toString(System.currentTimeMillis() / 1000L);
        sb.append(timestamp);
        sb.append(split_flag);
        sb.append(app_secret);
        // 使用拼接好的字符串使用 MD5 加密 二进制转化为大写的十六进制
        String sign = DigestUtils.md5DigestAsHex(sb.toString().getBytes());

//        System.out.println("sb:" + sb);
//        System.out.println("sign:" + sign);
//        System.out.println("timestamp:" + timestamp);

        String url = "https://fws.lhsr.sh.gov.cn/ywtg/api/edc/transferFillDataHour/save?app_key=" + app_key + "&sign=" + sign
                + "&timestamp=" + timestamp+ "&dtype=1" ;

        JSONObject jsonObject = new JSONObject();
        JSONArray array1 = new JSONArray();
//        JSONObject item1 = new JSONObject();
//        item1.put("tguid","485e959d-0e35-4de5-808c-ed1be9b4140a");
//        item1.put("transfername","泰和路转运站");
//        item1.put("disposal",12.91);
//        item1.put("sewageAmount",23.01);
//        item1.put("did","124");
//        item1.put("reportSite","485e959d-0e35-4de5-808c-ed1be9b4140a");
//        item1.put("reportStation","06");
//        item1.put("businessTime","2023-09-15 11:58:14");
//        item1.put("collectDate","2023-09-15 11:58:14");

        array1.add(item1);
        jsonObject.put("params",array1);
        System.out.println("推送数据执行：" + DateUtils.getTime());
        return HttpUtil.post(url, jsonObject.toJSONString());

    }


    private String sortAscii(SortedMap<?, ?> params) {
        StringBuffer sb = new StringBuffer();
        Iterator<?> itQueryString = params.keySet().iterator();
        try {
            while (itQueryString.hasNext()) {
                Object key = itQueryString.next();
                String list = (String) params.get(key);
                // 空值不传递，不参与签名组串
                if (StringUtils.isNotBlank(list)) {
                    sb.append(key);
                    sb.append(list);
                }
            }
            return sb.toString();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }
}
